const { Workorderhistory } = require('../../database/index')
const { getExecutedSql } = require('../../logger');
// 导出
module.exports = async (req, res) =>
{
    const operation = req.params.operation;
    const { workOrderID, statusChangeDate, newStatus, description } = req.body
    try {
        switch (operation) {
            case 'create':
                // // 根据客户端传递过来的 用户名 查询 数据库 中 是否 存在这用户名
                const state = {
                    WorkOrderID: workOrderID,
                    StatusChangeDate: statusChangeDate,
                    NewStatus: newStatus,
                    Description: description,
                }
                const newWorkOrderData = await Workorderhistory.create(state)
                const data = await Workorderhistory.findOne({
                    where: {
                        HistoryID: newWorkOrderData.HistoryID
                    }
                })
                res.status(200).send({
                    data,
                    meta: {
                        msg: "创建成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            case 'read':
                const workorderhistoryData = await Workorderhistory.findAll()
                res.status(200).send({
                    data: workorderhistoryData,
                    meta: {
                        msg: "获取成功",
                        status: 200
                    },
                    sql: getExecutedSql()
                })
                break;
            default:
                break;
        }

    } catch (error) {
        res.status(500).send({ message: 'Internal server error', error: error.message });
    }
}